<?php

define('SMARTY_DIR','./smarty/');
require_once SMARTY_DIR . 'Smarty.class.php';

$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;

require_once 'cf/init.inc';
require_once 'cf/session.inc';
require_once 'cf/params.inc';
require_once 'cf/page.inc';
require_once 'cf/gallery.inc';
require_once 'cf/settings.inc';

require_once 'cf/components/thumbnail.php';
require_once 'cf/components/text.php';
require_once 'cf/components/block.php';

session::open();

$lang = $par->asInt('lang');
if (!$lang) 
{
	if (session::has('lang')) 
	{
		$lang = session::get('lang');
	}
	else
	{
		$lang = 2;
	}
}

session::set('lang',$lang);

function setLang($lang)
{
	switch ($lang)
	{
		case 2:
			setlocale(LC_ALL,'En-Us');
			break;
	}
}

setLang($lang);

$smarty->assign('lang',$lang);

date_default_timezone_set('Europe/Moscow');


function show($template, $title='', $crumbs=array())
{
	global $smarty;

	$smarty->assign('title',$title);
	$smarty->assign('crumbs',$crumbs);
	$smarty->assign('tpl', "$template.tpl");

	$smarty->display('main.tpl');
}

$pages = new ChildPages(null);
$smarty->assign('pages',$pages);
$smarty->assign('bottomMenu',$pages->toArray());


function getTours($params)
{
   $where = '';
   foreach ($params as $nm => $val)
   {
      if ($where) {
         $where .= ' AND ';
      }
      else {
         $where = 'WHERE ';
      }
      
      $where .= "tours_schedule.$nm = $val";
   }

   return 
      query2arrays("
         SELECT 
                tours_schedule.id, 
                DATE_FORMAT(start_date,'%d.%m.%Y') AS start_date, 
                DATE_FORMAT(end_date,'%d.%m.%Y') AS end_date,
                price, 
                places_available, 
                boats.id AS boat_id, 
                boats.name AS boat_name, 
                routs.id AS rout_id, 
                routs.name AS rout_name,
				DATEDIFF(tours_schedule.end_date,tours_schedule.start_date) AS duration_days,
				YEAR(start_date)=YEAR(end_date) AS same_year,
				MONTH(start_date) AS m,
				MONTHNAME(start_date) AS month_name
         FROM tours_schedule
         INNER JOIN boats ON tours_schedule.boat = boats.id
         LEFT OUTER JOIN routs ON tours_schedule.rout = routs.id
         $where
         ORDER BY tours_schedule.start_date, tours_schedule.end_date
      ");
}

$img = new RandomGalleryImage('main_page');

$smarty->assign('mainImg',$img->image());


$smarty->assign(
	'routList',
	query2arrays("SELECT id, TRIM(name) AS name FROM routs ORDER BY sort_order, name")
);

$smarty->assign(
	'years',
	query2vector("
		SELECT DISTINCT year
		FROM (
		   SELECT DISTINCT YEAR(start_date) AS year FROM tours_schedule
		   UNION
		   SELECT DISTINCT YEAR(end_date) AS year FROM tours_schedule
		) t
		ORDER BY year 
	")
);


?>